package com.example.purplestarcb.shoujizhushou;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Created by PurpleStarCB on 2015/3/20.
 */
public class ActivityFragmentMain extends Activity implements View.OnClickListener {
    /**
     * 设置常用功能Fragment
     */
    private AccelerateFragment accelerateFragment;
    /**
     * 设置安全防护Fragment
     */
    private SecurityFragment securityFragment;
    /**
     * 设置软件管理Fragment
     */
    private SofewareFragment sofewareFragment;
    /**
     * 设置隐私保护Fragment
     */
    private ProfileFragment profileFragment;
    /**
     * 设置防盗保护Fragment
     */
    /**
     * 设置常用功能Layout
     */
    private View accelerateLayout;
    /**
     * 设置安全防护Layout
     */
    private View securityLayout;
    /**
     * 设置软件管理Layout
     */
    private View sofewareLayout;
    /**
     * 设置隐私保护Layout
     */
    private View profileLayout;
    /**
     * 在Tab布局显示常用功能控件
     */
    private ImageView accelerateImage;
    /**
     * 在Tab布局显示安全防护控件
     */
    private ImageView securityImage;
    /**
     * 在Tab布局显示软件管理控件
     */
    private ImageView sofewareImage;
    /**
     * 在Tab布局显示隐私保护控件
     */
    private ImageView profileImage;
    /**
     * 控件TextView
     */
    private TextView acceletateTextView;
    private TextView sofewareTextView;
    private TextView profileTextView;
    private TextView securityTextView;
    /**
     * 对Fragment进行管理
     */
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_action_tool_layout);
        //初始化布局元素
        intViews();
        fragmentManager = getFragmentManager();
        //第一次启动时选择第0个tab
        setTabSelection(0);

    }


    private void intViews() {

        accelerateLayout = findViewById(R.id.acceletate_layout);
        securityLayout = findViewById(R.id.security_layout);
        sofewareLayout = findViewById(R.id.sofeware_layout);
        profileLayout = findViewById(R.id.profile_layout);
        accelerateImage = (ImageView) findViewById(R.id.acceletate_image);
        securityImage = (ImageView) findViewById(R.id.security_image);
        sofewareImage = (ImageView) findViewById(R.id.sofeware_image);
        profileImage = (ImageView) findViewById(R.id.profile_image);
        acceletateTextView = (TextView) findViewById(R.id.acceletate_text);
        securityTextView = (TextView) findViewById(R.id.security_text);
        sofewareTextView = (TextView) findViewById(R.id.sofeware_text);
        profileTextView = (TextView) findViewById(R.id.profile_text);
        accelerateLayout.setOnClickListener(this);
        securityLayout.setOnClickListener(this);
        sofewareLayout.setOnClickListener(this);
        profileLayout.setOnClickListener(this);
        
        

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.acceletate_layout:
                // 当点击了常用软件tab时，选中第1个tab
                setTabSelection(0);
                break;
            case R.id.sofeware_layout:
                // 当点击了软件管理人tab时，选中第2个tab
                setTabSelection(1);
                break;
            case R.id.security_layout:
                // 当点击了安全防护时，选中第3个tab
                setTabSelection(2);
                break;
            case R.id.profile_layout:
                // 当点击了隐私保护时，选中第4个tab
                setTabSelection(3);
                break;
            default:
                break;
        }
    }

    private void setTabSelection(int index) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                // 当点击了常用功能tab时，改变控件的图片和文字颜色
                accelerateImage.setImageResource(R.drawable.tab_accelerate_selected);
                acceletateTextView.setTextColor(Color.parseColor("#87D442"));
                if (accelerateFragment == null) {
                    // 如果accelerateFragment为空，则创建一个并添加到界面上
                    accelerateFragment = new AccelerateFragment();
                    transaction.add(R.id.content, accelerateFragment);
                } else {
                    // 如果accelerateFragment不为空，则直接将它显示出来
                    transaction.show(accelerateFragment);
                }
                break;
            case 1:
                // 当点击了软件管理tab时，改变控件的图片和文字颜色
                sofewareImage.setImageResource(R.drawable.tab_sofeware_manager_selected);
                sofewareTextView.setTextColor(Color.parseColor("#87D442"));
                if (sofewareFragment == null) {
                    // 如果sofewareFragment为空，则创建一个并添加到界面上
                    sofewareFragment = new SofewareFragment();
                    transaction.add(R.id.content, sofewareFragment);
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(sofewareFragment);
                }
                break;
            case 2:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                securityImage.setImageResource(R.drawable.tab_security_tools_selected);
                securityTextView.setTextColor(Color.parseColor("#87D442"));
                if (securityFragment == null) {
                    // 如果securityFragment为空，则创建一个并添加到界面上
                    securityFragment = new SecurityFragment();
                    transaction.add(R.id.content, securityFragment);
                } else {
                    // 如果securityFragment不为空，则直接将它显示出来
                    transaction.show(securityFragment);
                }
                break;
            case 3:
            default:
                // 当点击了设置tab时，改变控件的图片和文字颜色
                profileImage.setImageResource(R.drawable.tab_profile_selected);
                profileTextView.setTextColor(Color.parseColor("#87D442"));
                if (profileFragment == null) {
                    // 如果profileFragment为空，则创建一个并添加到界面上
                    profileFragment = new ProfileFragment();
                    transaction.add(R.id.content, profileFragment);
                } else {
                    // 如果profileFragment不为空，则直接将它显示出来
                    transaction.show(profileFragment);
                }
                break;

        }
        transaction.commit();

    }

    /**
     * 清楚选中状态
     */
    private void clearSelection() {
        accelerateImage.setImageResource(R.drawable.tab_accelerate_normal);
        acceletateTextView.setTextColor(Color.parseColor("#82858b"));
        sofewareImage.setImageResource(R.drawable.tab_sofeware_manager_normal);
        sofewareTextView.setTextColor(Color.parseColor("#82858b"));
        securityImage.setImageResource(R.drawable.tab_security_tools_normal);
        securityTextView.setTextColor(Color.parseColor("#82858b"));
        profileImage.setImageResource(R.drawable.tab_profile_normal);
        profileTextView.setTextColor(Color.parseColor("#82858b"));
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (accelerateFragment != null) {
            transaction.hide(accelerateFragment);
        }
        if (sofewareFragment != null) {
            transaction.hide(sofewareFragment);
        }
        if (securityFragment != null) {
            transaction.hide(securityFragment);
        }
        if (profileFragment != null) {
            transaction.hide(profileFragment);
        }

    }

}
